<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRole"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="list">
          <el-table-column label="序号" align="center" type="index" width="120" />
          <el-table-column label="角色名称" prop="name" align="center" width="240" />
          <el-table-column label="描述" prop="description" align="center" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{row}">
              <el-button size="small" type="success" @click="showAssignPermission(row)">分配权限</el-button>
              <el-button size="small" type="primary" @click="edit(row)">编辑</el-button>
              <el-button size="small" type="danger" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="center" align="middle" style="height:60px;">
          <!-- current-page 当前页码
          page-size每页显示数量
          layout 分页组件的布局
          total 绑定总条数 用于计算应该有多少页 -->
          <el-pagination
            :current-page="params.page"
            :page-size="params.pagesize"
            layout="total,prev, pager, next"
            :total="total"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog :title="title" :visible="dialogVisible" @close="close">
        <!-- 表单内容 -->
        <el-form ref="roleRef" :model="roleForm" :rules="roleRules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部内容 -->
        <template #footer>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="ptimary" @click="submit">确定</el-button>
        </template>
      </el-dialog>
    </div>
    <Assignpermission ref="assignRef" :visible="assignPermissionVisible" :current-node="currentNode" @dialog-close="dialogclose" />
  </div>
</template>

<script>
import Assignpermission from './components/assign-permission.vue'
import { getRoleList, addRole, getRoleDetail, editRoleDetail, delRole } from '@/api'
export default {
  components: {
    Assignpermission
  },
  data() {
    return {
      // 当前需要分配权限的节点
      currentNode: {},
      list: [],
      params: {
        page: 1, // 页码
        pagesize: 10// 每页条数
      },
      total: 0, // 总条数
      dialogVisible: false, // 弹层显示与隐藏
      assignPermissionVisible: false, // 控制分配权限弹窗显示与隐藏
      roleForm: {
        name: '',
        description: ''
      },
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.fetchRoleList()
  },
  methods: {
    // 获取角色列表
    async fetchRoleList() {
      const { data: { total, rows }} = await getRoleList(this.params)
      this.list = rows
      this.total = total
    },
    // 显示第几页发生的变化
    pageChange(newpage) {
      // 将页码赋值当前的最新页码
      this.params.page = newpage
      // 获取最新的表格数据
      this.fetchRoleList()
    },
    // 新增角色
    addRole() {
      this.dialogVisible = true
    },
    // 关闭弹层
    close() {
      this.dialogVisible = false
      // 置空表单
      this.$refs.roleRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 提交表单新增角色
    submit() {
      // 1.兜底校验
      this.$refs.roleRef.validate(async(valid) => {
        if (!valid) return false
        if (this.roleForm.id) {
          // 编辑
          await editRoleDetail(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        // 3.提示用户
        this.$message.success(this.title + '成功')
        // 4.关闭弹窗
        this.close()
        // 5.刷新列表
        this.fetchRoleList()
      })
    },
    // 编辑角色
    async edit(val) {
      console.log(val)
      this.dialogVisible = true
      const { data } = await getRoleDetail(val.id)
      this.roleForm = data
    },
    // 删除功能
    del(id) {
      this.$confirm('确认删除么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 删除调用的接口
        await delRole(id)
        this.$message.success('删除成功')
        // 最后一页最后一条数据时，页数减 1
        if (this.list.length === 1 && this.params.page > 1) {
          this.params.page--
        }
        this.fetchRoleList()
      })
    },
    // 分配权限
    showAssignPermission(row) {
      this.currentNode = row
      this.$refs.assignRef.fetchPermisssionList(row.id)
      this.assignPermissionVisible = true
    },
    dialogclose() {
      this.assignPermissionVisible = false
    }
  }
}

</script>

<style>

</style>
